<%@ page import="BL.AccessDB"%>
<%@ page import="BL.Eintrag"%>

<%@ page contentType="text/html; charset=ISO-8859-1" language="java"%>
<%
HttpSession sess = request.getSession();
	int xv = 0;
	String plz = (String) sess.getAttribute("plz");//request.getParameter("plz");
	String ort = (String) sess.getAttribute("ort");// request.getParameter("ort");
	String strasse = (String) sess.getAttribute("strasse");// = request.getParameter("strasse");
	String hausnr = (String) sess.getAttribute("hausnr");// = request.getParameter("hausnr");
	String kat = (String) sess.getAttribute("kategorie");//request.getParameter("kategorie");
 	String adresse =strasse + " " + hausnr;
	String ortplz =plz + " " + ort;
	//System.out.println( BL.AccessDB.getErgebnisliste(kat)[0].getName() );
	Eintrag[] erg = null;
	/*if (kat != null) {
		erg = BL.AccessDB.getErgebnisliste(kat, 49.35, 9.14);
		
	}*/
%>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html {
	height: 100%
}

body {
	height: 100%;
	margin: 0px;
	padding: 0px
}

#map_canvas {
	height: 100%
}
</style>
<script type="text/javascript"
	src="https://maps.google.com/maps/api/js?sensor=true">
	
</script>
<script type="text/javascript">
	var geocoder;
	var map;
	var lat;
	var lng;
	var kat = "<%=kat%>";	

	function popup (url) {
		 fenster = window.open(url, "Detail Ansicht", "width=400,height=300,resizable=yes");
		 fenster.focus();
		 return false;
		}
	
	function codeAddress() {

		var ort = document.getElementById("ort").value;
		var address = document.getElementById("address").value;
		var add = ort + "," + address;


		geocoder = new google.maps.Geocoder();

  		geocoder.geocode({
			'address' : add
			
		}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				myOptions = {
					zoom : 13,
					center : results[0].geometry.location,
					mapTypeId : google.maps.MapTypeId.ROADMAP
				};
		  		lat = results[0].geometry.location.lat();
				lng = results[0].geometry.location.lng();

			} else {
				alert("Geocode was not successful for the following reason: "
						+ status);
			}
		});
  		downloadUrl("DatenbankZugriff?lat="+lat+"&lng="+lng+"&kat="+kat);


  		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  		

  				
		<%erg = (Eintrag[])session.getAttribute("Ergebnis");%>



		var marker;
<%if (erg != null) {%>
<%for (int i = 0; i < erg.length; i++) {
					out.print("myLatlng = new google.maps.LatLng("
							+ erg[i].getLat() + "," + erg[i].getLang() + ");");
					out.print("marker = new google.maps.Marker({map:map,position : new google.maps.LatLng("
							+ erg[i].getLat()
							+ ","
							+ erg[i].getLang()
							+ "),title : '"+erg[i].getName()+"'});");
					
					//out.print("google.maps.event.addListener(marker, 'click', function() {infowindow.open(map,marker);});");
					//System.out.println( erg[i].getName() );
				}%>
	
<%}%>

	// To add the marker to the map, call setMap();
	}
	
	
	function downloadUrl(url) {
	      var request = window.ActiveXObject ?
	          new ActiveXObject('Microsoft.XMLHTTP') :
	          new XMLHttpRequest;

	  

	      request.open('GET', url, true);
	      request.send(null);
	    }

	google.maps.event.addDomListener(window, 'load', codeAddress);
	
</script>
</head>
<div>
	<input type="hidden" id="ort" value="<%=ortplz%>"> <input
		type="hidden" id="address" value="<%=adresse%>">
</div>
<body>

	<jsp:include page="header.jsp" />
	<hr>
	<table>
		<tr>
			<th>Index</th>
			<th>Name</th>
			<th>Entfernung</th>
		</tr>
		<%if(erg != null){
			for (int x = 0; x < erg.length; x++) {
				out.println("<tr><td>" + (x + 1) + "</td>");
				out.println("<td>	<p><a href=detail.jsp?id="+erg[x].getID()+" target=_blank onclick=post popup(this.href);>"+erg[x].getName()+"</a></p></td>");
				out.println("<td>" + Math.round(erg[x].getEnt()*100)/100. +" km"+ "</td>");
			}
		}
		%>
	</table>

	<input type="button" id="karteAnzeigen" value="Karte anzeigen" onclick="codeAddress()">
	<div id="map_canvas" style="width: 100%; height: 70%;"></div>


	<p>
	<p>
	<p>
	<form method=post action="centralServlet">
		<input type="hidden" name=act value="suchen"></input> <input
			type="submit" value="Neue Suche">
	</form>
	<p>
	<p>
	<hr>
	<jsp:include page="footer.jsp" />
	</form>


</html>

<script>
setTimeout("codeAddress()",500);
</script>
